/**
 * Created by steve on 14-9-18.
 */
define(['jquery', 'dialogs'], function ($, dia) {
    var url_base = 'http://120.24.55.7:8080/staff/';

    return {
        get: function (url, parameters, fail, success) {
            console.log('---请求参数---');
            console.log('url:');
            console.log(url);
            console.log('parameters:');
            console.log(parameters);

            // 链接参数到url
            var full_url = url_base + url + "?";
            var i = 0;
            $.each(parameters, function (key, value) {
                if (i == 0) {
                    full_url += key + "=" + value;
                } else {
                    full_url += "&" + key + "=" + value;
                }
            });

            // GET
            $.ajax({
                type: 'GET',
                url: full_url,
                dataType: 'json',
                beforeSend: function(xhr) {
                    if (localStorage.getItem('login_user')!=null) {
                        var user = JSON.parse(localStorage.getItem('login_user'));
                        console.log('user');
                        console.log(user);
                        xhr.setRequestHeader('BasicAuthUsername', user.userId);
                    }
                }
            }).fail(function (error) {
                console.error(error);
                dia.alert('Oops!', 'No internet connection. Please check your network.', ['OK'], function (title) {
                });
                fail('Please check your network.');
            }).done(function (response) {
                console.log(response);
                success(response);
            });
        },

        post: function (url, parameters, fail, success) {
            console.log('---POST Request---');
            console.log('url: %o', url);
            console.log('parameters: %o', parameters);
            // POST
            $.ajax({
                type: 'POST',
                url: url_base + url,
                dataType: 'json',
                data: parameters,
                beforeSend: function(xhr) {
                    if (localStorage.getItem('login_user')!=null) {
                        var user = JSON.parse(localStorage.getItem('login_user'));
                        xhr.setRequestHeader('BasicAuthUsername', user.userId);
                    }
                }
            }).fail(function (error) {
                console.error(error);
                dia.alert('Oops!', 'No internet connection. Please check your network.', ['OK'], function (title) {

                });
                fail('Please check your network.');
            }).done(function (response) {
                console.log('POST Response: %o', response);
                console.assert(response.code != null, '返回数据没有code字段');
                console.assert(response.msg != null, '返回数据没有msg字段');
                console.assert(response.data != null, '返回数据没有data字段');
                success(response);
            });
        } ,

        code : {
            error: -1,
            countineActivity: 10000
        }
    }
});